package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.AddressDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.BaiduMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: myitcast_health
 * @ClassName: AddressServiceImpl
 * @description:
 * @author: xiechangyu
 * @create: 2020-08-27 23:38
 **/
@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    public PageInfo<Address> findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Address> list = addressDao.findPage(queryPageBean.getQueryString());
        PageInfo<Address> info = new PageInfo<>(list);
        return info;
    }


    public void addAddress(String addressname) {
        Map<String, Double> lngAndLat = BaiduMapUtils.getLngAndLat(addressname);
        Address address = new Address();
        Double lng = lngAndLat.get("lng");
        Double lat = lngAndLat.get("lat");
        address.setAddressname(addressname);
        address.setLatitude(lat);
        address.setLongitude(lng);
        addressDao.addAddress(address);

    }

    @Override
    public void deleteAddress(int id) {
        addressDao.deleteAddress(id);
    }

    @Override
    public List<String> findAllAddress() {
        List<Address> list = addressDao.findAllAddress();
        List<String> stringList = new ArrayList<>();
        for (Address address : list) {
            String addressname = address.getAddressname();
            stringList.add(addressname);
        }
        return stringList;
    }

    //判断地址是否已存在
    public boolean ifExist(String addressName) {
        if(addressDao.findAddress(addressName)!=null){
            return false;
        }
        return true;
    }

    @Override
    public List<Address> findAllAddress2() {
        return addressDao.findAllAddress2();
    }
}
